package com.caochenlei.singleton.mode4;

public class SingletonTest {
    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton1 == singleton2);
    }
}

//懒汉式
class Singleton {
    //私有构造方法，防止外部new
    private Singleton() {}

    //提供一个私有的静态变量对象
    private static Singleton instance;

    //向外部提供一个公开获取方法
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
